feat: add cast_ptr_sized_int lint#16262
Conversation
|
rustbot has assigned @samueltardieu. Use |
This comment has been minimized.
This comment has been minimized.
|
Lintcheck changes for 3a6e6b3
This comment will be updated if you push new changes |
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
This is exactly the gap the lint addresses: If you compile on 64-bit, usize as u64 looks safe. But the same code behaves differently on 32-bit. No existing lint catches this architecture-dependent behavior. The suggestion is not directly applicable: should i remove the suggestion (just warn without providing a fix)? |
|
The lint still triggers inconsistently. For example:
And indeed, no fix should be provided if it would be incorrect, but the help message may suggest using |
fa57af9 to
fba9c01
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
There was a problem hiding this comment.
If you add
#![deny(clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]at the top of your test file, you'll see that most of the lines that your PR lints will already be linted.
I wonder if it would be more useful to extend existing lints with an additional configuration option to warn when pointer sized ints could cause issues even though they don't on the platform on which they are compiled.
Maybe the user could even specify the range of sizes it is interested in compiling for. For example, target_usize_supported_bounds = [32, 64] would let the user indicate that they are not interested in compiling for 16 bit platforms, and the lint should report an error if the current platform is outside this range.
What do you think?
tests/ui/cast_ptr_sized_int.rs
Outdated
| @@ -0,0 +1,89 @@ | |||
| //@no-rustfix | |||
| #![warn(clippy::cast_ptr_sized_int)] | |||
| #![allow(clippy::unnecessary_cast)] | |||
There was a problem hiding this comment.
Sorry; just keep #![warn(clippy::cast_ptr_sized_int)]
tests/ui/cast_ptr_sized_int.stderr
Outdated
| @@ -0,0 +1,229 @@ | |||
| error: casting `usize` to `u8`: the behavior depends on the target pointer width | |||
There was a problem hiding this comment.
The message is not great here: casting usize to u8 will always truncate.
Implement a new lint to detect casts between pointer-sized integers (`usize`, `isize`) and fixed-size integers that exhibit architecture-dependent behavior.
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I think the
With proposed Error message with Benefits: No duplication. User control. Leverages existing code |
|
@rustbot ready |
Introduce a new restriction lint to identify casts between pointer-sized integer types (
usize,isize) and fixed-size integer types.Encourage the use of
TryFromorTryIntoover directascasts to make potential platform-specific truncation or zero-extension explicit and handled.Also Provide machine-applicable suggestions to replace risky casts with fallible conversions while excluding constant contexts where such traits are not yet stable.
Closes: #9231
changelog: add cast_ptr_sized_int lint